package com.yztc.yidoutang.fragment.sousuo_fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;
import com.yztc.yidoutang.R;
import com.yztc.yidoutang.activity.result_activity.ResultActivity;
import com.yztc.yidoutang.bean.SearchHistoryBean;
import com.yztc.yidoutang.utils.DBHelper;
import com.yztc.yidoutang.utils.MySqliteOpenHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/10/18.
 */

public class SouSuoFragment extends Fragment implements SouSuoContract.View {
    private FlowLayout flowLayout,hot_fl;
    private EditText searchEditText;
    private SouSuoContract.Presenter presenter = new SouSuoPresenter(this);
    private MySqliteOpenHelper helper;
    private Dao<SearchHistoryBean, Integer> searchHistoryDao;
  /*  @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }*/

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sousuo, null);
        initHelper();
        searchEditText = (EditText) getActivity().findViewById(R.id.search_edittext);
        flowLayout = (FlowLayout) view.findViewById(R.id.fl);
        hot_fl= (FlowLayout) view.findViewById(R.id.hot_fl);
        initHotSearchView();
        presenter.getHotSearchData();
        initSearchEvent();
        return view;
    }


    public int getLayoutId() {
        return R.layout.fragment_sousuo;
    }

    public void initSearchEvent() {
        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String words=searchEditText.getText().toString().trim();
                    if (!TextUtils.isEmpty(words)) {
                        List<SearchHistoryBean> querylist = DBHelper.queryOne(helper, words, getActivity());
                        if (querylist.size() == 0) {
                            DBHelper.storeSearchHistory(words, helper, getActivity());
                        }
                        ((InputMethodManager) searchEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                        Intent intent = new Intent(getActivity(), ResultActivity.class);
                        intent.putExtra("keywords", words);
                        startActivity(intent);
                    }else {
                        Toast.makeText(getActivity(),"请输入关键词",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

    }

    @Override
    public void onGetHotSearchSuccess(List<String> hotSearchList) {
        initFlowLayout(hotSearchList,flowLayout);
    }

    @Override
    public void onGetHotSearchFail(String msg) {

    }

    //初始化流式布局中热门标签的数据
    public void initFlowLayout(List<String> list,FlowLayout fl) {
        for (int i = 0; i < list.size(); i++) {
            final TextView textView = new TextView(getActivity());
            textView.setText(list.get(i));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(25, 10, 10, 25);
            textView.setLayoutParams(lp);
            textView.setTextSize(15f);
            textView.setTextColor(getResources().getColor(R.color.clr_gray_dark));
            textView.setGravity(Gravity.CENTER);
            textView.setSingleLine(true);
            textView.setBackgroundResource(R.drawable.text_flag);
            textView.setId(i);
            fl.addView(textView);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ResultActivity.class);
                    intent.putExtra("keywords", textView.getText());
                    startActivity(intent);
                }
            });
        }
    }
    //初始化数据库连接
    public void initHelper(){
        helper=MySqliteOpenHelper.getInstance(getActivity());
    }

    public void initHotSearchView(){
        List<String> hotList=new ArrayList<>();
        List<SearchHistoryBean> resultList=DBHelper.selectAll(helper);
        Log.i("aaa","------"+resultList.size());
        if (resultList!=null){
            for (int i=0;i<resultList.size();i++){
                hotList.add(resultList.get(i).getQueryWords());
            }
            initFlowLayout(hotList,hot_fl);
        }

    }
}
